package cn.fenqing168.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * nio 复制文件
 *
 * @author fenqing
 */
public class NioFileChannel4 {

    public static void main(String[] args) {

        try (
                //创建一个文件输入流
                //创建一个文件输出流
                FileInputStream fis = new FileInputStream("./nio/nio-test-image.jpeg");
                FileOutputStream fos = new FileOutputStream("./nio/nio-test-image-copy.jpeg")
        ) {
            //从输入流中获取一个channel
            FileChannel inChannel = fis.getChannel();
            //从输出流中获取一个channel
            FileChannel outChannel = fos.getChannel();
            //创建一个byteBuffer
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            //直接使用Buffer的transferFrom方法，进行通道之间的拷贝
            outChannel.transferFrom(inChannel, 0, inChannel.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
